/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Provides actions for built in DOMercury Items
 * 
 * --CHANGELOG--
 * 
 * */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ManagedWinapi.Windows;
using DOMercury.Interfaces;
using DOMercury.Items;
using DOMercury.Managers;


namespace DOMercury.Actions
{
    class MyComputerOpenAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Open"; }
        }

        public string Description
        {
            get { return "Open My Computer in Windows Explorer"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                System.Diagnostics.Process.Start("explorer.exe", @"/n, /e, ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}");
                return true;
            }
            catch
            {
                return false;
            }
            
        }

        #endregion
    }

    //compmgmt.msc
    class MyComputerManageAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Manage"; }
        }

        public string Description
        {
            get { return "Manage My Computer"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                System.Diagnostics.Process.Start("compmgmt.msc");
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class DOMercuryShowOptionsAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Show Options Form"; }
        }

        public string Description
        {
            get { return "Show the DOMercury Options Form"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                DOMercuryProgramFlowManager.Instance.ShowOptions();
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class DOMercuryQuitAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Quit"; }
        }

        public string Description
        {
            get { return "Exit DOMercury"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                DOMercuryProgramFlowManager.Instance.Exit();
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class DesktopOpenAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Open Directory"; }
        }

        public string Description
        {
            get { return "Open Desktop in Windows Explorer"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                System.Diagnostics.Process.Start("explorer.exe", System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class ShowDesktopAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "ShowDesktop"; }
        }

        public string Description
        {
            get { return "Show Desktop"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                System.Diagnostics.Process.Start("Plugins\\ShowDesktop.scf");
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class RefreshIndexAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Refresh Index"; }
        }

        public string Description
        {
            get { return "Refresh the Index"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                DOMercury.Managers.IndexManager.RebuildIndex();
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class StartScreenSaverAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Start"; }
        }

        public string Description
        {
            get { return "Starts the ScreenSaver"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                Win32.SendMessage(ManagedWinapi.Windows.SystemWindow.DesktopWindow.HWnd, (int)Win32.Msgs.WM_SYSCOMMAND, new IntPtr(Win32.SC_SCREENSAVE), new IntPtr(0));
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class ChangeScreenSaverAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Change"; }
        }

        public string Description
        {
            get { return "Change the ScreenSaver"; }
        }

        public string ParamDescription
        {
            get { return "Unused"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                System.Diagnostics.Process.Start("desk.cpl");
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class DomGrepAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Grep"; }
        }

        public string Description
        {
            get { return "Grep the top folder or file using DomGrep"; }
        }

        public string ParamDescription
        {
            get { return "Regular Expression (Optional)"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                ItemsList items = (ItemsList)item;
                string reg = (string)specialParams;

                if (reg.Trim().Length > 0)
                {
                    System.Diagnostics.Process.Start("DomGrep", "/we " + items.Items[0].Path + " " + specialParams);
                }
                else
                {
                    System.Diagnostics.Process.Start("DomGrep", "/we " + items.Items[0].Path);
                }
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion
    }

    class ChangePriorityAction : IAction
    {
        #region IAction Members

        public string ActionType
        {
            get { return "Change Priority"; }
        }

        public string Description
        {
            get { return "Change the Priority of the Item"; }
        }

        public string ParamDescription
        {
            get { return "The new priority of the item (Higher numbers mean increased priority)"; }
        }

        public string ParameterType
        {
            get { return "CommandLine"; }
        }

        public bool Perform(IItem item, object specialParams)
        {
            int newP = 0;
            try
            {
                newP = Convert.ToInt32(((string)(specialParams)));
                IndexManager.ChangePriority(item.DisplayName, item.DisplayDesc, newP - 1);

            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("PriorityChangeFailed, must be between " + (int.MinValue).ToString() + " and " + (int.MaxValue).ToString());
            }

            return true;

        }

        #endregion
    }

}
